package com.dries.portal.module.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dries.common.api.CommonResult;
import com.dries.portal.module.user.entity.Address;
import com.dries.portal.module.user.enums.AddressDefault;
import com.dries.portal.module.user.form.AddressForm;
import com.dries.portal.module.user.form.EditDefaultAddressForm;
import com.dries.portal.module.user.service.IAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 用户地址表
 *
 * @author dries
 * @email samphsanie@gmail.com
 * @date 2020-09-04 16:26:01
 */
@RestController
@Api(value = "AddressController", tags = "用户地址管理API")
@RequestMapping("user/address")
public class AddressController {

    @Resource
    private IAddressService addressService;

    /**
     * 列表
     */
    @ApiOperation("用户地址列表")
    @GetMapping("/list/{userId}")
    public CommonResult list(@PathVariable String userId) {

        List<Address> addresses = addressService.list(new QueryWrapper<Address>()
                .lambda().eq(Address::getUserId, userId)
                .orderByDesc(Address::getModifiedTime)
        );

        return CommonResult.success(addresses);
    }


    /**
     * 信息
     */
    @ApiOperation("用户地址信息详情")
    @GetMapping("/info/{id}")
    public CommonResult info(@PathVariable("id") Integer id) {
        Address address = addressService.getById(id);
        return CommonResult.success(address);
    }

    /**
     * 保存
     */
    @ApiOperation("新增用户地址信息")
    @PostMapping("/save")
    public CommonResult save(@RequestBody AddressForm form) {

        Address address = new Address();
        BeanUtils.copyProperties(form, address);
        addressService.save(address);

        return CommonResult.success();
    }

    /**
     * 修改用户地址
     */
    @ApiOperation("修改用户地址信息")
    @PutMapping("/update")
    public CommonResult update(@RequestBody Address address) {

        if (address.getIsDefault() != null && address.getIsDefault().equals(AddressDefault.DEFAULT.getCode())) {

            Address address1 = addressService.getOne(new QueryWrapper<Address>().lambda()
                    .eq(Address::getUserId, address.getUserId())
                    .eq(Address::getIsDefault, AddressDefault.DEFAULT.getCode())
            );
            if (address1 != null) {
                addressService.update(new UpdateWrapper<Address>()
                        .lambda()
                        .set(Address::getIsDefault, AddressDefault.NO_DEFAULT.getCode())
                        .eq(Address::getId, address1.getId())
                );
            }

        }

        addressService.updateById(address);
        return CommonResult.success();
    }

    /**
     * 修改用户默认地址
     */
    @ApiOperation("修改用户默认地址")
    @PostMapping("/default")
    public CommonResult setDefaultAddress(@RequestBody EditDefaultAddressForm form) {

        // 查找默认地址
        Address address1 = addressService.getOne(new QueryWrapper<Address>().lambda()
                .eq(Address::getUserId, form.getUserId())
                .eq(Address::getIsDefault, AddressDefault.DEFAULT.getCode())
        );
        if (address1 != null) {
            address1.setIsDefault(AddressDefault.NO_DEFAULT.getCode());
            addressService.updateById(address1);
        }

        addressService.update(new UpdateWrapper<Address>()
                .lambda().set(Address::getIsDefault, form.getStatus())
                .eq(Address::getId, form.getId())
        );

        return CommonResult.success();
    }

    /**
     * 删除
     */
    @ApiOperation("删除用户地址")
    @DeleteMapping("/delete")
    public CommonResult delete(@RequestBody Integer[] ids) {
        addressService.removeByIds(Arrays.asList(ids));
        return CommonResult.success();
    }

}
